#pragma once

#include "mission.h"
#include "mission_manager.h"
#include "mission_event.h"
#include "userAPI.h"

using namespace AI9;

class move_mission : public mission
{
public:
	move_mission(mission *parent_mission, const gtype_list &troop, Position target) : mission(parent_mission, troop) 
	{
		/*
			move_mission only handles the case that one general moves to one target
		 */
		m_target = target;
	}

	virtual void execute()
	{
		mission_manager::get_intance()->set_general_mission(m_troop.front(), this);
		if (mission_manager::get_intance()->actioned_state() == false && mission_manager::get_intance()->active_general() == m_troop.front())
			event_arrived(new basic_mission_event(this));
	}

	virtual void event_arrived(mission_event *e)
	{
		assert(e->get_type() == "basic_mission_event");
		basic_mission_event *bme = (basic_mission_event *)e;
		UserAPI &api = mission_manager::get_intance()->get_api();
		Position start = api.GetCntArmy()->pos;

		if (api.HamDistance(start, m_target) == 0)
		{
			upcall_event(new mission_completed_event(this, false));
			return;
		}

		mission_manager::get_intance()->set_actioned_state(true);
		api.Move(mission_manager::get_intance()->get_action_queue(), start, m_target);
	}

	virtual void cancel()
	{
		//	 do nothing
	}

	virtual std::string get_type() { return "move_mission"; };

	Position m_target;
};